﻿Public Class FormCategory

    Private Sub FormCategory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        GUIHelper.FormatDataGridView(dgvCategory)

        cboCategoryName.ValueMember = "TypeId"
        cboCategoryName.DisplayMember = "TypeName"
        cboCategoryName.DataSource = Sql.ExecuteDataTable("SELECT TypeId,TypeName FROM Type WHERE Active=1 UNION SELECT 0,N'គ្រប់ប្រភេទ'")
        LoadData()

    End Sub

    Public Sub LoadData()
        Dim x = "SELECT C.*, T.TypeName FROM Category C INNER JOIN Type T ON T.TypeId=C.TypeId WHERE (C.CategoryName LIKE N'%" & txtSearch.Text & "%' OR T.TypeName LIKE N'%" & txtSearch.Text & "%') AND C.Active=1"
        If (cboCategoryName.SelectedValue <> 0) Then
            x = x & " AND T.TypeId=" & cboCategoryName.SelectedValue
        End If 
        Me.dgvCategory.DataSource = Sql.ExecuteDataTable(x)
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        LoadData()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If Me.dgvCategory.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("តើលោកអ្នកចង់លុបក្រុមនៃប្រភេទទំនិញមួយនេះមែនទេ?", "លុបក្រុមនៃប្រភេទ", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If

        Dim row As DataGridViewRow = Me.dgvCategory.SelectedRows(0)
        Dim id = row.Cells("CategoryId").Value

        Sql.ExecuteNonQuery("UPDATE Category SET Active=0 WHERE CategoryId=" & id)

        LoadData()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim frm As New FormCategoryEditor()
        frm.cboTypeName.SelectedValue = cboCategoryName.SelectedValue
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If Me.dgvCategory.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row As DataGridViewRow = Me.dgvCategory.SelectedRows(0)
        Dim CategoryId = row.Cells("CategoryId").Value
        Dim CategoryName = row.Cells("CategoryName").Value
        Dim TypeName = row.Cells("TypeId").Value

        Dim frm As New FormCategoryEditor()
        frm.txtCategoryId.Enabled = False
        frm.txtCategoryId.Text = CategoryId
        frm.txtCategoryName.Text = CategoryName
        frm.cboTypeName.SelectedValue = TypeName

        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If

    End Sub

    Private Sub cboCategoryName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCategoryName.SelectedIndexChanged
        LoadData()
    End Sub

End Class